import { setToken, getToken } from '@/utils/auth'
import { loginApi, getUserInfoApi } from '@/api'
// vuex存储token、 存储请求函数
const state = {
  loginToken: getToken() || null,
  userInfo: {}
}
const mutations = {
  saveToken(state, payload) {
    setToken(payload)
    state.loginToken = payload
  },
  saveUserInfo(state, payload) {
    state.userInfo = payload
  }
}
const actions = {
  async reqToken(context, payload) {
    const res = await loginApi(payload)
    context.commit('saveToken', res.data.data)
  },
  async reqUserInfo(context) {
    const res = await getUserInfoApi()

    context.commit('saveUserInfo', res.data.data)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
